package com.edu.whty.controller;

import com.edu.whty.common.constants.CodeConstants;
import com.edu.whty.entity.base.ResponseEntity;
import com.edu.whty.entity.comment.LabelReq;
import com.edu.whty.entity.comment.ResourcLabel;
import com.edu.whty.service.LabelService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/label")
public class LabelController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private LabelService labelService;

    /**
     * 标签列表
     * by zhujg
     *
     * @param request
     * @param response
     * @param label
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ResponseEntity list(HttpServletRequest request, HttpServletResponse response,
                               @RequestBody LabelReq label) {
        try {
            if (null == label) {
                return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "参数不能为空");
            } else {
                if (StringUtils.isEmpty(label.getLoginPlatformCode())) {
                    return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "loginPlatformCode不能为空");
                }
                if (StringUtils.isEmpty(label.getBusinessId())) {
                    return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "businessId不能为空");
                }
            }
            List<ResourcLabel> list = labelService.getResourceLabelList(label);
            Map<String, Object> mapList = new HashMap<String, Object>();
            mapList.put("list", list);
            return ResponseEntity.getSuccessResponse(mapList);
        } catch (Exception e) {
            // 捕捉异常构造返回信息
            logger.error("获取标签列表发生异常" + e.getMessage(), e);
            return ResponseEntity.getFailedResponse(CodeConstants.CONSTANT_RETURN_CODE_SYSTEM_ERROR, "获取标签列表异常");
        }
    }
}
